When you create an application, there are certain unknown elements. For example, if the users tries to save the current file which is on a floppy, and the floppy is not inserted when your application attempts to access it, your application will crash and you would lose any unsaved information. Error handling intercepts these errors, so you can give the user a useful error message, instead of your application crashing. i.e. instead of getting a message "Err 45 Disk not ready" and then the application crashing, you could change your code so that the user would get the message "Please insert a floppy disk into Drive A", and then give the user an option to retry or cancel.

When you write code, you may want your application to totally ignore any errors that occur in a certain procedure. You can do this by entering

On Error Resume Next

at the start point, where you want it to ignore errors. To re enable the error messages in that procedure type

On Error Goto 0

For example, the following code attempts to delete Test.txt from drive C:. The first attempt, errors are ignored, but the second time, an error occurs when the procedure attempts to delete the file (providing the file does not exist).

Sub Command1_Click()
   On Error Resume Next ' Ignore all errors from this point

   Kill "C:\test.txt"
   ' An error occurs if the file does not exist, but it is ignored
   ' The error number and message is printed to the debug window
   Debug.Print Err & " : " & Error 

   On Error GoTo 0 ' Errors from this point onwards will cause a break
   
   Kill "C:\test.txt"
   ' An error occurs, and a message is displayed. Code execution stops. 

End Sub

Err returns the last error number that occurred. Error returns the last error message. Kill deletes the specified file

All errors can be identified by a unique error id. You can use this id to tell which error occurred, and to give a suitable error message. The simplest way to catch and error is the On Error Resume Next statement. When an error occurs, the error number is stored in the Variable Err, and code execution continues as if nothing has happened. If you know an error is likely to occur on a certain line, you can write some code which can catch this error.

The following code attempts to delete the file C:\test.txt. If it does not exist it displays a message saying so.

Sub Command1_Click
   On Error Resume Next ' Enable error trapping
   
   Kill "C:\test.txt"
   If Err = 53 Then ' The File Not Found error occurred
      Msgbox "C:\test.txt was not found"
   End If
End Sub

The error code for File Not Found is 53, and as that was the last error to occur, it was stored in the variable Err.

In a complex procedure, you will find there are lots of places where errors can occur, and quite often are the same errors. If this is the case putting code after each line is a waste of space and time. A much more efficient way is to use the statement:

On Error GoTo LineLabel

This statements tell visual basic, that if an error occurs after this statement, to continue code execution at the LineLabel specified. The following code attempts to delete A: est.txt (this assumes A: is a disk drive). If an error occurs, it will give the appropriate error message. It will then attempt to get the size of C:fred.txt . Again, if an error occurs it will give the appropriate message. However it all uses the same code to detect the error.

Sub Command1_Click()
   ' If an error occurs GoTo FileError
   On Error GoTo FileError
   
   ' Attempt to delete A: est.txt
   Kill "A: est.txt"

   ' Attempt to get the size of C: est.txt
   gFileSize = FileLen("A: est.txt")

   Msgbox "File Size is: " & gFileSize

   ' Exit the procedure as no error has occurred
   Exit Sub

FileError:
   Select Case Err
   Case 53
       ' The file does not exist. Inform the user
      Msgbox "The file does not exist"
      gFileSize = "<Unknown>"
   Case 5
      ' The disk is not ready. Prompt user for action
      Ans = Msgbox ("Insert a disk into Drive A", vbAbortRetryIgnore)
      If Ans = vbRetry Then 
         ' The user chose Retry. Resume code execution
         ' at where the error occurred
         Resume
     ElseIf Ans=vbIgnore Then
       ' The user chose to ignore. Resume code execution
         ' at the line after where the error occurred
         Resume Next
      Else
         ' The user chose abort. Exit this procedure
         Exit Sub
      End If
   Case Else
        ' unexpected error
      Msgbox "An error occurred. " & Err & " : " & Error
   End Select
End Sub

FileLen returns the size of the specified file in bytes. Normally Err 75 would occur if the drive was not ready, however when using FileLen Err 5 occurs